#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int main(int argc, char **argv)
{

    int shmid;
    printf(" pid = %d \n", getpid());
    shmid = shmget((size_t)0x5000, 10240, 0666 | IPC_CREAT);
    if (shmid  == -1)
    {
        perror("share memory is fail");
        exit(1);
    };

    char *ptext = NULL;
    ptext = (char *)shmat(shmid, 0, 0);
    printf("写入前 %s \n", ptext);
    printf("可以运行到 %d 行\n", __LINE__);

    sprintf(ptext, "本程序的进程号是：%d", getpid());
    printf("可以运行到 %d 行\n", __LINE__);
    printf("%s",ptext);
    // shmdt(ptext);
    // shmctl(shmid,IPC_RMID,0);

    return 0;
}